package com.hs.gas.client;

import gnu.io.CommPortIdentifier;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ConfigDialog extends org.eclipse.swt.widgets.Dialog implements MouseListener, DisposeListener {

	private Shell dialogShell;
	private Group groupDepartment;
	private Group groupHost;
	private Button btnCancle;
	private Composite compositeBtn;
	private Button btnSave;
	private List listPort;
	private Label labelPortName;
	private Group groupTimer;
	private Text txtContext;
	private Label labelContext;
	private Text txtHostUrl;
	private Label labelHostUrl;
	private Text txtHostPort;
	private Label labelHostPort;
	private Text txtHostProtocal;
	private Label labelHostProtocal;
	private Text txtDepartmentId;
	private Label labelDepartment;

	private Config config;

	private Locale locale = new Locale("zh","CN");
	private ResourceBundle resource = ResourceBundle.getBundle("messages", locale);

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Dialog inside a new Shell.
	*/
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			ConfigDialog inst = new ConfigDialog(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ConfigDialog(Shell parent, int style) {
		super(parent, style);
		config = new Config();
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
			GridLayout dialogShellLayout = new GridLayout();
			dialogShellLayout.makeColumnsEqualWidth = true;

			dialogShell.setLayout(dialogShellLayout);
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setSize(452, 399);
			dialogShell.setText(resource.getString("Config"));
			{
				groupDepartment = new Group(dialogShell, SWT.NONE);
				GridLayout groupDepartmentLayout = new GridLayout();
				groupDepartmentLayout.makeColumnsEqualWidth = true;
				groupDepartmentLayout.numColumns = 2;
				groupDepartment.setLayout(groupDepartmentLayout);
				GridData groupDepartmentLData = new GridData();
				groupDepartmentLData.widthHint = 263;
				groupDepartmentLData.heightHint = 35;
				groupDepartment.setLayoutData(groupDepartmentLData);
				groupDepartment.setText(resource.getString("DepartmentInfo"));
				{
					labelDepartment = new Label(groupDepartment, SWT.NONE);
					labelDepartment.setText(resource.getString("DepartmentId"));
				}
				{
					GridData txtDepartmentIdLData = new GridData();
					txtDepartmentIdLData.widthHint = 118;
					txtDepartmentIdLData.heightHint = 22;
					txtDepartmentId = new Text(groupDepartment, SWT.NONE);
					txtDepartmentId.setLayoutData(txtDepartmentIdLData);
					txtDepartmentId.setText(config.get(Config.KEY_DEPARTMENT_ID));
				}
			}
			{
				groupHost = new Group(dialogShell, SWT.NONE);
				GridLayout groupHostLayout = new GridLayout();
				groupHostLayout.makeColumnsEqualWidth = true;
				groupHostLayout.numColumns = 2;
				groupHost.setLayout(groupHostLayout);
				GridData groupHostLData = new GridData();
				groupHostLData.widthHint = 301;
				groupHostLData.heightHint = 124;
				groupHost.setLayoutData(groupHostLData);
				groupHost.setText(resource.getString("HostInfo"));
				{
					labelHostProtocal = new Label(groupHost, SWT.NONE);
					labelHostProtocal.setText(resource.getString("HostProtocal"));
				}
				{
					GridData txtHostProtocalLData = new GridData();
					txtHostProtocalLData.widthHint = 118;
					txtHostProtocalLData.heightHint = 22;
					txtHostProtocal = new Text(groupHost, SWT.NONE);
					txtHostProtocal.setLayoutData(txtHostProtocalLData);
					txtHostProtocal.setText(config.get(Config.KEY_HOST_PROTOCAL));
				}
				{
					labelHostPort = new Label(groupHost, SWT.NONE);
					labelHostPort.setText(resource.getString("HostPort"));
				}
				{
					GridData txtHostPortLData = new GridData();
					txtHostPortLData.widthHint = 119;
					txtHostPortLData.heightHint = 25;
					txtHostPort = new Text(groupHost, SWT.NONE);
					txtHostPort.setLayoutData(txtHostPortLData);
					txtHostPort.setSize(119, 22);
					txtHostPort.setText(config.get(Config.KEY_HOST_PORT));
				}
				{
					labelHostUrl = new Label(groupHost, SWT.NONE);
					labelHostUrl.setText(resource.getString("HostUrl"));
				}
				{
					txtHostUrl = new Text(groupHost, SWT.NONE);
					GridData txtHostUrlLData = new GridData();
					txtHostUrlLData.widthHint = 119;
					txtHostUrlLData.heightHint = 22;
					txtHostUrl.setLayoutData(txtHostUrlLData);
					txtHostUrl.setText(config.get(Config.KEY_HOST_URL));
				}
				{
					labelContext = new Label(groupHost, SWT.NONE);
					labelContext.setText(resource.getString("HostContext"));
				}
				{
					txtContext = new Text(groupHost, SWT.NONE);
					GridData txtContextLData = new GridData();
					txtContextLData.widthHint = 119;
					txtContextLData.heightHint = 22;
					txtContext.setLayoutData(txtContextLData);
					txtContext.setText(config.get(Config.KEY_CONTEXT));
				}
			}
			{
				groupTimer = new Group(dialogShell, SWT.NONE);
				GridLayout groupTimerLayout = new GridLayout();
				groupTimerLayout.makeColumnsEqualWidth = true;
				groupTimerLayout.numColumns = 2;
				groupTimer.setLayout(groupTimerLayout);
				GridData groupTimerLData = new GridData();
				groupTimerLData.widthHint = 315;
				groupTimerLData.heightHint = 77;
				groupTimer.setLayoutData(groupTimerLData);
				groupTimer.setText(resource.getString("TimerInfo"));
				{
					labelPortName = new Label(groupTimer, SWT.NONE);
					labelPortName.setText(resource.getString("TimerPort"));
				}
				{
					listPort = new List(groupTimer, SWT.NONE);

					Enumeration<CommPortIdentifier> en = CommPortIdentifier.getPortIdentifiers();
					if(en != null) {
						while(en.hasMoreElements()) {
							CommPortIdentifier comm = en.nextElement();
							listPort.add(comm.getName());
						}
					}
					if(config.get(Config.KEY_TIMER_PORT) != null) {
						String[] selection = {config.get(Config.KEY_TIMER_PORT)};
						GridData listPortLData = new GridData();
						listPortLData.widthHint = 117;
						listPortLData.heightHint = 64;
						listPort.setLayoutData(listPortLData);
						listPort.setSelection(selection);
					}
				}
			}
			{
				compositeBtn = new Composite(dialogShell, SWT.NONE);
				GridLayout compositeBtnLayout = new GridLayout();
				compositeBtnLayout.makeColumnsEqualWidth = true;
				compositeBtnLayout.numColumns = 2;
				compositeBtnLayout.marginLeft = 70;
				GridData compositeBtnLData = new GridData();
				compositeBtnLData.widthHint = 327;
				compositeBtnLData.heightHint = 39;
				compositeBtn.setLayoutData(compositeBtnLData);
				compositeBtn.setLayout(compositeBtnLayout);
				{
					btnSave = new Button(compositeBtn, SWT.PUSH | SWT.CENTER);
					btnSave.addMouseListener(this);
					btnSave.setText(resource.getString("Save"));
				}
				{
					btnCancle = new Button(compositeBtn, SWT.PUSH | SWT.CENTER);
					btnCancle.setText(resource.getString("Cancle"));
					btnCancle.addMouseListener(this);
				}
			}
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
	 */
	public void mouseDoubleClick(MouseEvent e) {
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
	 */
	public void mouseDown(MouseEvent e) {
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
	 */
	public void mouseUp(MouseEvent e) {
		if(btnSave == e.getSource()) {
			config.set(Config.KEY_CONTEXT, txtContext.getText());
			config.set(Config.KEY_DEPARTMENT_ID, txtDepartmentId.getText());
			config.set(Config.KEY_HOST_PORT, txtHostPort.getText());
			config.set(Config.KEY_HOST_PROTOCAL, txtHostProtocal.getText());
			config.set(Config.KEY_HOST_URL, txtHostUrl.getText());
			String[] selected = listPort.getSelection();
			if(selected != null && selected.length == 1) {
				config.set(Config.KEY_TIMER_PORT, listPort.getSelection()[0]);
			}
			config.saveConfig();
		} else if(btnCancle == e.getSource()) {
			this.dialogShell.close();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
	 */
	public void widgetDisposed(DisposeEvent e) {
		System.exit(0);
	}
}
